//
//  IFAABioViewController.m
//  test
//
//  Created by dingding on 16/10/2017.
//  Copyright © 2017 esandinfo. All rights reserved.
//

#import "SimpleIFAAViewController.h"
#import "SimpleIFAAProcess.h"
#import "HomePageViewController.h"
#import <esand_cloud_ifaa/esand_cloud_ifaa.h>

@interface SimpleIFAAViewController ()

@end

@implementation SimpleIFAAViewController
{
    UILabel *_tvLabel;
    UISwitch *_switchBtn;

    NSString *labelString;
    UITextView *textView;
    
    UISwitch *_patternSwitchBtn;
    
    int authType;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //TODO 开启客户端日志上传
    [LogManager startUpLog];
    
    //导航栏高度
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    //导航栏+状态栏高度
    CGFloat topHeight = self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height;
    
    //初始化登录以及支付的话术文本框
    NSString *supportAuthType = [IFAAManager isSupportIFAA];
    if ([supportAuthType isEqualToString:IFAA_STATUS_FINGERPRINT]) {
        labelString = @"指纹认证";
        authType = AUTHTYPE_FINGERPRINT;
    } else if ([supportAuthType isEqualToString:IFAA_STATUS_FACE_ID]) {
        labelString = @"人脸认证";
        authType = AUTHTYPE_FACEID;
    }
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置认证按钮布局
    button.frame = CGRectMake(10, topHeight + 20 , SCREEN_WIDTH-20, 50) ;
    [button setTitle:labelString forState:UIControlStateNormal];
    //设置按钮文字颜色
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithRed:239.0/255.0 green:137.0/255.0 blue:47.0/255.0 alpha:1.0]];
    //按钮圆角
    [button.layer setCornerRadius:20.0];
    //设置按钮点击事件
    [button addTarget:self action:@selector(verify:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, topHeight + 160, SCREEN_WIDTH - 20, SCREEN_HEIGHT - topHeight - 150)];
    // Do any additional setup after loading the view, typically from a nib.
    textView = [[UITextView alloc] initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y,scrollView.frame.size.width, scrollView.frame.size.height)];
    textView.pagingEnabled = YES;
    textView.clipsToBounds = YES;
    textView.editable = NO;
    [self.view addSubview:textView];
    
    UIButton * clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置清理日志按钮布局
    clearBtn.frame = CGRectMake(0, SCREEN_HEIGHT- 70, SCREEN_WIDTH, 70) ;
    //设置按钮文字颜色
    [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [clearBtn setBackgroundColor:[UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0]];
    //设置按钮点击事件
    [clearBtn addTarget:self action:@selector(Clear:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearBtn];
}

-(IBAction)setUp:(id)sender{
    
}

- (void)back:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

// IFAA 认证
-(IBAction)verify:(id)sender {
    SimpleIFAAProcess* ifaaProcess = [[SimpleIFAAProcess alloc] initWithVController:self authtype:authType];
    [ifaaProcess auth];
}

// 清理textview 打印的数据
- (IBAction)Clear:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [textView setText:@""];
    });
    
}

- (void)logMsg:(NSString*)content {
    if (content == NULL) {
        return;
    }
    content = [NSString stringWithFormat:@"%@ \n", content];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:content];
        [string addAttribute:NSForegroundColorAttributeName
                       value:[UIColor blackColor]
                       range:NSMakeRange(0, content.length)];
        
        [string addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:15]
                       range:NSMakeRange(0 , content.length)];
        NSMutableAttributedString* tvStr = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
        
        [tvStr appendAttributedString:string];
        [textView setAttributedText:tvStr];
    });
}

@end

